home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / hockey.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  6.9 KB  |  243 lines

  1. #ifndef _global_h
  2. #    include "global.h"
  3. #endif
  4.  
  5. #ifndef _hockey_h
  6. #    include "hockey.h"
  7. #endif
  8. #ifndef _arcs_h
  9. #    include "arcs.h"
  10. #endif
  11. #ifndef _ball_h
  12. #    include "ball.h"
  13. #endif
  14. #ifndef _goal_h
  15. #    include "goal.h"
  16. #endif
  17. #ifndef _wall_h
  18. #    include "wall.h"
  19. #endif
  20. #ifndef _graph_h
  21. #    include "graph.h"
  22. #endif
  23. #ifndef _mover_h
  24. #    include "mover.h"
  25. #endif
  26.  
  27. void Hockey::InitTable() {
  28.     mid = Vec2( AreaOffX()+AreaWidth()/2.0, AreaOffY()+AreaHeight()/2.0 );
  29.     post[0][0] = new StaticBall( AreaOffX(),AreaOffY()+(AreaHeight()-GoalSize)/2.0 );
  30.     post[0][1] = new StaticBall( AreaOffX(),AreaOffY()+(AreaHeight()+GoalSize)/2.0 );
  31. //    goal[0]    = new Goal( post[0][0]->P(), post[0][1]->P() );
  32.     goal[0] = new Goal(    Vec2( AreaOffX()-GetNormalBallSize(), AreaOffY() ),
  33.                                 Vec2( AreaOffX()-GetNormalBallSize(), AreaOffY()+AreaHeight() ) );
  34.     post[1][0] = new StaticBall( AreaOffX()+AreaWidth(),AreaOffY()+(AreaHeight()-GoalSize)/2.0 );
  35.     post[1][1] = new StaticBall( AreaOffX()+AreaWidth(),AreaOffY()+(AreaHeight()+GoalSize)/2.0 );
  36. //    goal[1]    = new Goal( post[1][0]->P(), post[1][1]->P() );
  37.  
  38.     goal[1] = new Goal(    Vec2( AreaOffX()+AreaWidth()+GetNormalBallSize(), AreaOffY() ),
  39.                                 Vec2( AreaOffX()+AreaWidth()+GetNormalBallSize(), AreaOffY()+AreaHeight() ) );
  40.  
  41.     w[0][0] = new Wall( post[0][0]->P(), Edge(0) );
  42.     w[0][1] = new Wall( Edge(0), Edge(1) );
  43.     w[0][2] = new Wall( Edge(1), post[1][0]->P() );
  44.  
  45.     w[1][0] = new Wall( post[0][1]->P(), Edge(3) );
  46.     w[1][1] = new Wall( Edge(3), Edge(2) );
  47.     w[1][2] = new Wall( Edge(2), post[1][1]->P() );
  48. }
  49.  
  50.  
  51. void Hockey::InitArea( double wx, double wy ) {
  52.     area_width  = wx;
  53.     area_height = wy;
  54.     area_off_x = (MaxX()-area_width)/2.;
  55.     area_off_y = (MaxY()-GoalHeight-area_height)/2.;
  56. }
  57.  
  58.  
  59. Hockey::Hockey(double wx, double wy) :
  60.     Game(wx+2.*FrameOffset,wy+2.*FrameOffset+GoalHeight)
  61. {
  62.     goal[0] = goal[1] = 0;
  63.  
  64.     InitArea(wx,wy);
  65.  
  66.     table_col            = SetMainBgColor( "white" );
  67.     red_bg_col            = AddBgColor( "red" );
  68.     blue_bg_col            = AddBgColor( "blue" );
  69.  
  70.     disc_col                = AddBallColor( "black" );
  71.     hand_col                = AddBallColor( "MidnightBlue" );
  72.  
  73.     AddShadeColor( "White", 1, 1 );
  74.     SetCursorColor( "brown" );
  75.  
  76.     keeper = new LineKeeper(GoalHeight,GoalFrame,blue_bg_col,CLOSE_LEFT|CLOSE_RIGHT);
  77.  
  78.     mdisc = 0;
  79.     mhand = 0;
  80.     disc = hand1 = hand2 = 0;
  81.     disc_in_goal = 0;
  82. }
  83.  
  84. Hockey::~Hockey() {
  85. int i,j;
  86.  
  87.     if (goal[0]) {
  88.         for (i=0;i<2;i++) {
  89.             delete goal[i];
  90.             for (j=0;j<2;j++)            delete post[i][j];
  91.             for (j=0;j<3;j++)            delete w[i][j];
  92.         }
  93.     }
  94.     if (mdisc)        delete mdisc;
  95.     if (mhand)        delete mhand;
  96.  
  97.     if (disc)        delete disc;
  98.     if (hand1)        delete hand1;
  99.     if (hand2)        delete hand2;
  100. }
  101.  
  102.  
  103. const Real & Hockey::GetPresetA() const {    return PresetA;}
  104. const Real & Hockey::GetPresetHaft() const {    return PresetHaft; }
  105. const Real & Hockey::GetSlowGranularity() const { return SlowGranularity; }
  106.  
  107. const Real & Hockey::GetNormalBallSize() const  { return DiscRadius; }
  108.  
  109. const Real Hockey::AreaOffX() const            { return area_off_x; }
  110. const Real Hockey::AreaOffY() const            { return area_off_y; }
  111. const Real Hockey::AreaWidth() const        { return area_width; }
  112. const Real Hockey::AreaHeight() const        { return area_height; }
  113.  
  114.  
  115.  
  116. void Hockey::InitPlayground() {
  117.     mdisc = new DiscMover( DiscRadius );
  118.     mdisc->Init();
  119.     DBG2(UnixTrace, "Hockey: Radius set back from %g to %g\n",
  120.                     (double)DiscRadius, (double)mdisc->GetActRadius() );
  121.     DiscRadius = mdisc->GetActRadius();
  122.  
  123.     mhand = new DiscMover( HandRadius );
  124.     mhand->Init();
  125.     DBG2(UnixTrace, "Hockey: Radius set back from %g to %g\n",
  126.                     (double)HandRadius, (double)mhand->GetActRadius() );
  127.     HandRadius = mhand->GetActRadius();
  128.  
  129.     Game::InitPlayground();
  130.     InitTable();
  131.  
  132.     disc = new Ball( mid, DiscRadius, DiscWeight );
  133.     disc->state = new BallState( mdisc, disc_col, disc->P() );
  134.  
  135.     hand1 = new Ball( AreaOffX()+AreaWidth()/4., AreaOffY()+AreaHeight()/2.0,
  136.                                 0.0, 0.0, HandRadius, HandWeight );
  137.     hand1->state = new BallState( mhand, hand_col, hand1->P() );
  138.     hand1->type  = Object::HandObj;
  139.  
  140.     hand2 = new Ball( AreaOffX()+AreaWidth()*3./4., AreaOffY()+AreaHeight()/2.0,
  141.                                 0.0, 0.0, HandRadius, HandWeight );
  142.     hand2->state = new BallState( mhand, hand_col, hand2->P() );
  143.     hand2->type  = Object::HandObj;
  144. }
  145.  
  146.  
  147. void Hockey::DrawBackground() const {
  148.     Game::DrawBackground();
  149.  
  150.     SetBgColor( table_col );
  151.     FillRectangle( AreaOffX(), AreaOffY(), AreaWidth(), AreaHeight() );
  152.     FillRectangle( post[0][0]->PX()-GetNormalBallSize(), post[0][0]->PY(),
  153.                             AreaWidth()+2.*GetNormalBallSize(), GoalSize );
  154.  
  155.     SetBgColor( blue_bg_col );            FillCircle( mid, 10.0 );
  156.     SetBgColor( table_col );         FillCircle( mid, 9.5 );
  157.     SetBgColor( blue_bg_col );
  158.     FillRectangle( AreaOffX()+AreaWidth()/2.0-0.25,AreaOffX(),0.5,AreaHeight() );
  159.     SetBgColor( red_bg_col );        FillCircle( mid, 3.0 );
  160.  
  161.     for (int i=0;i<4;i++) {
  162.         Vec2    cmid = (mid+Edge(i))/2.0;
  163.         SetBgColor( blue_bg_col );            FillCircle( cmid, 5.0 );
  164.         SetBgColor( table_col );         FillCircle( cmid, 4.5 );
  165.     }
  166.     SetBgColor( blue_bg_col );
  167.     FillRectangle( AreaOffX()+AreaWidth()/4.0-0.25,AreaOffX(),0.5,AreaHeight() );
  168.     FillRectangle( AreaOffX()+AreaWidth()*3.0/4.0-0.25,AreaOffX(),0.5,AreaHeight() );
  169.     SetBgColor( red_bg_col );
  170.     for (i=0;i<4;i++) {
  171.         FillCircle( (mid+Edge(i))/2.0, 2.0 );
  172.     }
  173. }
  174.  
  175.  
  176. void Hockey::InPocket( Ball *b ) {
  177.     Game::InPocket(b);
  178.     if (b==disc) {
  179.         disc_in_goal = 1;
  180.     }
  181. }
  182.  
  183. void Hockey::StopBall( Ball *b ) {
  184.     Game::StopBall(b);
  185.     if (b==disc && disc_in_goal) {
  186.         disc_in_goal = 0;
  187.  
  188. Real    to;    // Endpunkt fⁿr aktuelle Kugel in der Pocketline
  189. Real    from;    // Startpunkt fⁿr aktuelle Kugel in der Pocketline
  190. Real    vy;    // Startgeschwindigkeit, damit Kugel in Mitte (to) liegenbleibt
  191.  
  192.         to        = AreaOffY() + AreaHeight() / 2.0;
  193.         from    = AreaOffY() + AreaHeight() - g->GetNormalBallSize()/2.0;
  194.         vy        = sqrt( 2.0*fabs((to-from)*b->a) );
  195.  
  196.         b->SetP( Vec2( AreaOffX() + AreaWidth()/2.0, from ) );
  197.         b->SetV( Vec2(RealZero,-vy) );
  198.     }
  199. }
  200.  
  201. //
  202.  
  203. TestHockey::TestHockey(double s) :
  204.     Hockey(), shot_speed(s)
  205. {
  206.     w[0] = 0;
  207. }
  208.  
  209. TestHockey::~TestHockey() {
  210.     if (w[0])    for(int i=0;i<4;i++)        delete w[i];
  211. }
  212.  
  213. const Real &TestHockey::GetPresetA() const                { return PresetA; }
  214. const Real &TestHockey::GetSlowGranularity() const    { return SlowGranularity; }
  215.  
  216. #if (0)
  217. void TestHockey::InitTable() {
  218.     w[0] = new Wall( AreaOffX(),                    AreaOffY(),
  219.                                         AreaOffX()+AreaWidth(), AreaOffY() );
  220.     w[1] = new Wall( AreaOffX()+AreaWidth(),    AreaOffY(),
  221.                                         AreaOffX()+AreaWidth(), AreaOffY()+AreaHeight() );
  222.     w[2] = new Wall( AreaOffX()+AreaWidth(),    AreaOffY()+AreaHeight(),
  223.                                         AreaOffX(),                    AreaOffY()+AreaHeight() );
  224.     w[3] = new Wall( AreaOffX(),                    AreaOffY()+AreaHeight(),
  225.                                         AreaOffX(),                    AreaOffY() );
  226. }
  227. #endif
  228.  
  229. void TestHockey::InitPlayground() {
  230.     mdisc = new DiscMover( DiscRadius );
  231.     mdisc->Init();
  232.     DBG2(UnixTrace, "Hockey: Radius set back from %g to %g\n",
  233.                     (double)DiscRadius, (double)mdisc->GetActRadius() );
  234.     DiscRadius = mdisc->GetActRadius();
  235.  
  236.     InitTable();
  237.  
  238.     disc = new Ball( Mid(3), DiscRadius, DiscWeight );
  239.     disc->state = new BallState( mdisc, disc_col, disc->P() );
  240.     disc->v = Vec2(shot_speed,RealZero);
  241. }
  242.  
  243.